﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace Library
{
    public partial class SearchForm : Form
    {
        LibraryEntities1 ins = new LibraryEntities1();
        public int bookid;
        public static ObjectSet<Book> books;
        public static ObjectSet<Reader> readers;
        public static ObjectSet<InstanceBook> instanceBooks;
        Reader reader;
        public SearchForm(Reader reader)
        {
            InitializeComponent();
            books = ins.Book;
            readers = ins.Reader;
            instanceBooks = ins.InstanceBook;
            this.reader = reader;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name = TBName.Text;

            foreach (var x in books)
            {
                int i = 0;
                if (x.BookName.ToUpper().Contains(name.ToUpper()))
                {

                        bookView.Rows.Add();
                        bookView.Rows[i].Cells[0].Value = x.bookid;
                        bookView.Rows[i].Cells[1].Value = x.ISBN;
                        bookView.Rows[i].Cells[2].Value = x.BookName;
                        bookView.Rows[i].Cells[3].Value = x.Location;
                        i++;

                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (bookView.CurrentRow != null)
            {
                bookid = Convert.ToInt32(bookView.CurrentRow.Cells[0].Value);
                Book book = books.Where(x => x.bookid.Equals(bookid)).FirstOrDefault();
                List <InstanceBook> instBook = new List<InstanceBook>();
                instBook = book.InstanceBook.ToList<InstanceBook>();

                InstanceBook bookDelivery = new InstanceBook();
                int available = 0;
                bool alreadyHas = false;

                foreach (var x in instBook)
                {
                    if (x.readerId == null) 
                    {
                        available++;
                        if (!Convert.ToBoolean(x.Control))
                            bookDelivery = x;
                    }
                    if (x.readerId == reader.ReaderId)
                        alreadyHas = true;
                }
                if (alreadyHas)
                    MessageBox.Show("Такая книга уже была выдана");
                else
                {
                    if (available < 2)
                    {
                        MessageBox.Show("Нет доступных для выдачи книг.");
                    }
                    else
                    {
                        InstanceBook bookToSave = instanceBooks.Where(x => x.InventoryNumber.Equals(bookDelivery.InventoryNumber)).FirstOrDefault();

                        bookToSave.readerId = reader.ReaderId;
                        ins.SaveChanges();
                        this.Close();
                    }
                }

            }
            else
            {
                MessageBox.Show("Выберите книгу");
            }
           // 
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SearchForm_Load(object sender, EventArgs e)
        {

            bookView.Columns.Add("bookId", "bookId");
            bookView.Columns["bookId"].Visible = false;


            bookView.Columns.Add("ISBN", "ISBN");
            bookView.Columns["ISBN"].ReadOnly = true;
            bookView.Columns["ISBN"].Width = 100;

            bookView.Columns.Add("Name", "Название");
            bookView.Columns["Name"].Width = 200;
            bookView.Columns["Name"].ReadOnly = true;

            bookView.Columns.Add("Location", "Место");
            bookView.Columns["Location"].Width = 200;
            bookView.Columns["Location"].ReadOnly = true;
        }
    }
}
